<% /* $Id: header.jsp 132 2011-08-26 10:23:42Z altdotua@gmail.com $ */ %>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.security.Principal" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Vector" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.ResourceBundle" %>
<%@ page import="java.lang.reflect.Field" %>

<%@ page import="org.w3c.dom.Document" %>
<%@ page import="org.w3c.dom.Element" %>
<%@ page import="org.w3c.dom.NodeList" %>

<%@ page import="org.apache.commons.lang.StringEscapeUtils" %>

<%@ page import="alt.djudge.frontend.server.datatypes.*" %>
<%@ page import="alt.djudge.frontend.server.datatypes.scores.*" %>
<%@ page import="alt.djudge.frontend.server.models.*" %>
<%@ page import="alt.djudge.frontend.server.models.scores.*" %>
<%@ page import="alt.djudge.frontend.server.utils.*" %>
<%@ page import="alt.djudge.frontend.server.*" %>
<%@ page import="alt.djudge.frontend.server.Deployment" %>
<%@ page import="alt.djudge.frontend.server.HibernateUtil" %>
<%@ page import="alt.djudge.frontend.server.servlets.DJudgeUtils" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.users.UserService" %>

<%@page import="alt.djudge.frontend.server.servlets.AbstractServlet"%>
<%@page import="alt.djudge.frontend.server.servlets.AbstractDataServlet"%>


<%
	try
	{
		Long refreshInterval = Long.parseLong(request.getParameter("refresh"));
		response.setHeader("Refresh", "" + refreshInterval);
	}
	catch(Exception e) {}
	
	String localeSid = request.getParameter("setlocale");
	if (localeSid != null)
	{
		LocalizationModel.setUserLocale(request, response, localeSid);
	}
	
	StringBuffer requestUrlBuffer = new StringBuffer(request.getRequestURL().toString());
	boolean fFirst = true;
    for (Enumeration<String> e = request.getParameterNames() ; e.hasMoreElements() ;)
    {
    	String paramName = e.nextElement();
    	if (!"setlocale".equals(paramName) && !"msg".equals(paramName))
    	{
	        requestUrlBuffer.append(fFirst ? '?' : '&');
	        requestUrlBuffer.append(paramName);
	        requestUrlBuffer.append('=');
	        requestUrlBuffer.append(URLEncoder.encode(request.getParameter(paramName), AbstractDataServlet.defaultEncoding));
	        fFirst = false;
    	}
    }
	String requestUrl = requestUrlBuffer.toString();
	String requestUrlEncoded = URLEncoder.encode(requestUrl, AbstractDataServlet.defaultEncoding);
%>



<%@page import="java.util.Enumeration"%><html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
		<title>DJudge Web Frontend</title>	
		<meta name="description" content="DJudge">
		<meta name="keywords" content="ACM, ICPC, IOI, algorithm, olympiad, judge, DJudge, online, problem set, programming, contest, topcoder">	
		<link rel="stylesheet" href="<%= Linker.link("css/unpriv.css") %>" type="text/css"/>
	</head>
	<body>
<%

	List<LocaleEntry> locales = LocalesModel.getAllEntries();

	ResourceBundle bundle = LocalizationModel.getResourceBundle();
	
	String msg = request.getParameter("msg");
	Long pageGenerationStarted = new Date().getTime();

	UserEntry user = UsersModel.getCurrentUser(request);
	
	String tcurrentContestId = request.getParameter("contest_id");
	ContestEntry contest = ContestsModel.getEntry(Long.parseLong(tcurrentContestId));
	
	if (user == null)
	{
		response.sendRedirect(Linker.link("login.jsp?return=" + URLEncoder.encode(request.getRequestURI().substring(1), "UTF-8") + "?contest_id=" + request.getParameter("contest_id")));
		return;
	}

	if (contest == null)
	{
		response.sendRedirect(Linker.link("contests/index.jsp"));
		return;
	}
	
	if (!contest.isAuthorizedUser(user))
	{
		response.sendRedirect(Linker.link("contests/index.jsp?msg=" + URLEncoder.encode("You are not allowed to access this (" + contest.getSid() + ") contest", AbstractServlet.defaultEncoding)));
		return;
	}
	ContestUserEntry contestUser = ContestUsersModel.getOrCreateEntry(contest.getId(), user.getId());

	Long pageGenerationHeaderFinished = new Date().getTime();
%>

<!-- <%= "" + ((pageGenerationHeaderFinished - pageGenerationStarted)) + " ms" %>  -->


<div id="container">
	<div id="l12">
		<div class="main_phrase"><%= contest.getName() %></div>
		<div class="user_actions">
			<table class="menu">
				<tr>
				<%
					for (LocaleEntry locale : locales)
					{
				%>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="
								<%= requestUrl %>&setlocale=<%= locale.getJavaLocale() %>"><%= !locale.equals(LocalizationModel.getUserLocale(user, request)) ? locale.getSid() : "[" + locale.getSid() + "]" %></a>
						</div>
					</td>
				<%
					}
				%>
					<td class="menu" width="50">
						<div class="contest_actions_item">
						</div>
					</td>				
				
<%
	if (!contestUser.getRole().startsWith("guest") && !contestUser.getRole().startsWith("board"))
	{
%>
 					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/profile.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_SETTINGS") %></a>
						</div>
					</td>
<%
	}
%>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("login?action=logout&return=" + URLEncoder.encode(Linker.link("contests/index.jsp"), AbstractServlet.defaultEncoding)) %>"><%= bundle.getString("W_LOGOUT") %> [<%= user.getUsername() %>]</a>
						</div>
					</td>
				</tr>
			</table>
		</div>
		<div class="white_empty_block">&nbsp;</div>
<%
	if (contestUser.isJudge())
	{
%>
		<div class="contest_actions">
			<table class="menu">
				<tr>
					<td class="menu">
						<div class="contest_actions_item">
							Admin:
		<a style="margin: 5px" href="<%= Linker.link("") %>">ROOT</a>
		<a style="margin: 5px" href="<%= Linker.link("olymp2011/") %>">CONTEST</a>
						</div>
					</td>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/admin/settings.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_SETTINGS") %></a>
						</div>
					</td>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/admin/data.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_SETTINGS") + "/Data" %></a>
						</div>
					</td>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/admin/users.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_USERS") %></a>
						</div>
					</td>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/admin/submissions.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_SUBMISSIONS") %></a>
						</div>
					</td>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/admin/problems.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_PROBLEMS") %></a>
						</div>
					</td>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/admin/languages.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_LANGUAGES") %></a>
						</div>
					</td>
				</tr>
			</table>
		</div>

<%
	}
%>
		<div class="white_empty_block">&nbsp;</div>
		<div class="contest_actions">
			<table class="menu">
				<tr>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/home.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_INFORMATION") %></a>
						</div>
					</td>
<%
	if (contest.isStarted(user) || contestUser.isJudge())
	{
		if (contestUser.canSubmit())
		{
%>					
								<td class="menu">
									<div class="contest_actions_item">
										<a class="menu" href="<%= Linker.link("contests/problems.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_PROBLEMS") %></a>
									</div>
								</td>
<%
		}
		if ((!contest.isFinished(user) && contestUser.canSubmit()) || contestUser.isJudge())
		{
%>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/submit.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_SUBMIT") %></a>
						</div>
					</td>
<%
		}
		if (contestUser.canSubmit())
		{
%>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/submissions.jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_SUBMISSIONS") %></a>
						</div>
					</td>
<%
		}
%>
					<td class="menu">
						<div class="contest_actions_item">
							<a class="menu" href="<%= Linker.link("contests/monitor_" + contest.getContestType().toLowerCase() + ".jsp?contest_id=" + contest.getId()) %>"><%= bundle.getString("W_MONITOR") %></a>
						</div>
					</td>
<%
	}
%>
				</tr>
			</table>
		</div>
	</div>
	<div id="l11"><img src="<%= Linker.link("/img/acm.gif") %>" alt="acm logo"/></div>
	
	<div id="l13">
		<div class="server_status_on">
			<div id="currentTime"><%= contest.isStarted(user) ? (contest.isFinished(user) ? Formatters.formatContestTime((contest.getFinishTime().getTime() - contest.getStartTime().getTime()) / 60000) : contest.getContestTimeString()) : " Starts in " + Formatters.formatContestTime((contest.getStartTime().getTime() - new Date().getTime()) / 60000) %></div>
			 / <b><%= contest.getState() %></b><%= contest.isStarted(user) && !contest.isFinished(user) ? "/ Remaining: " + Formatters.formatContestTimeSeconds((contest.getFinishTime().getTime() - new Date().getTime()) / 1000)  : "" %>
		</div>
